import sys
import numpy as np

sys.path.append("../utils")

from utils import OpRunner, verify_result

def main():
    func = OpRunner("./build/libop.so", 3, pint_time=True)
    minvalue = 1
    maxvalue = 10
    shape = [1024]
    # for dtype in [np.float16, np.float32, np.int32, np.int8]:
    for dtype in [np.float16, np.float32]:
        print('测试', dtype)
        dy = np.random.uniform(minvalue, maxvalue, shape).astype(dtype)
        input_x = np.random.uniform(minvalue, maxvalue, shape).astype(dtype)
        output = np.zeros(shape).astype(dtype)
        attr = 1.702
        attr_opp = 0 - attr
        attr_half = attr / 2
        abs_x = np.abs(input_x) # |x|
        mul_abs_x = abs_x * attr_opp # -1.702|x|
        exp_x = np.exp(mul_abs_x) # e^(-1.702|x|)
        add_2 = input_x * exp_x * attr # x * e^(-1.702|x|) * 1.702
        exp_pn_x = np.exp((input_x - abs_x) * attr) # e ^ ((x-|x|) * 1.702)
        div_up = exp_x + add_2 +exp_pn_x # e^(-1.702|x|) + x * e^(-1.702|x|) * 1.702 + e ^ ((x-|x|) * 1.702)
        div_down = (exp_x + 1) ** 2 # (e^(-1.702|x|) + 1) ** 2
        res = div_up / div_down 
        golden = dy * res
        
        func([dy, input_x, output], [2])
        verify_result(output, golden)
        print("----------------------------")
    


if __name__ == "__main__":
    main()
